package demo.database;

import java.sql.Connection;

import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.db.RecordSet;

/**
 *
 *
 * @author 周成功
 *
 */
public class TruncateAllTable {
    public static void main(String[] args) {
        String[][] config = new String[][] {
//                {"127.0.0.1", "1521", "ORCL", "etms3_base", "etms3_base"},
//                {"127.0.0.1", "1521", "ORCL", "etms3", "etms3"}
                {"127.0.0.1", "1521", "ORCL", "nbas", "nbas"}
        };
        
        Connection conn = null;
        try {
            String sqlAllTable = "select t.tname from tab t where t.tabtype='TABLE' and tname not in (SELECT object_name FROM user_recyclebin)";

            for (int i = 0; i < config.length; i++) {
                conn = DBUtils.getOracleConnection(config[i][0], config[i][1], config[i][2], config[i][3], config[i][4], true);
                RecordSet record = DBUtils.executeQueryToRecordSet(conn, sqlAllTable);
                while(record.moveNext()) {
                    String tablename = record.getString("tname");
                    DBUtils.execute(conn, "alter table " + tablename + " disable all triggers");
                    DBUtils.execute(conn, "truncate table " + tablename);
                    DBUtils.execute(conn, "alter table " + tablename + " enable all triggers");
                }
                DBUtils.close(conn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn);
        }
    }
}

